package com.tech.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

public class HttpURLUtils {
	protected static Logger LOG = LoggerFactory.getLogger(HttpURLUtils.class);

	public static Map<String, Object> doPost(String requestUrl,
			Map<String, Object> reqParams) throws Exception {
		return doPost(requestUrl, reqParams, "GBK", false);
	}

	public static Map<String, Object> doGet(String requestUrl,
			Map<String, Object> reqParams) throws Exception {
		return doGet(requestUrl, reqParams, "GBK", false);
	}

	public static String doStringPost(String requestUrl,
			Map<String, Object> reqParams) throws Exception {
		return execute(requestUrl, reqParams, "POST", "GBK", false);
	}

	public static String doStringGet(String requestUrl,
			Map<String, Object> reqParams) throws Exception {
		return execute(requestUrl, reqParams, "GET", "GBK", false);
	}

	public static Map<String, Object> doHttpRequestPost(String requestUrl,
			Map<String, Object> reqParams) throws Exception {
		return doPost(requestUrl, reqParams, "GBK", true);
	}

	public static Map<String, Object> doHttpRequestGet(String requestUrl,
			Map<String, Object> reqParams) throws Exception {
		return doGet(requestUrl, reqParams, "GBK", true);
	}

	public static String doStringHttpRequestPost(String requestUrl,
			Map<String, Object> reqParams) throws Exception {
		return doStringPost(requestUrl, reqParams, "GBK", true);
	}

	public static String doStringHttpRequestGet(String requestUrl,
			Map<String, Object> reqParams) throws Exception {
		return doStringGet(requestUrl, reqParams, "GBK", true);
	}

	@SuppressWarnings("unchecked")
	public static Map<String, Object> doPost(String requestUrl,
			Map<String, Object> reqParams, String encoding,
			boolean isHttpRequest) throws Exception {
		String content = execute(requestUrl, reqParams, "POST", encoding,
				isHttpRequest);
		return JsonUtils.strToObject(content, Map.class);
	}

	@SuppressWarnings("unchecked")
	public static Map<String, Object> doGet(String requestUrl,
			Map<String, Object> reqParams, String encoding,
			boolean isHttpRequest) throws Exception {
		String content = execute(requestUrl, reqParams, "GET", encoding,
				isHttpRequest);
		return JsonUtils.strToObject(content, Map.class);
	}

	public static String doStringPost(String requestUrl,
			Map<String, Object> reqParams, String encoding,
			boolean isHttpRequest) throws Exception {
		return execute(requestUrl, reqParams, "POST", encoding, isHttpRequest);
	}

	public static String doStringGet(String requestUrl,
			Map<String, Object> reqParams, String encoding,
			boolean isHttpRequest) throws Exception {
		return execute(requestUrl, reqParams, "GET", encoding, isHttpRequest);
	}

	private static String execute(String requestUrl,
			Map<String, Object> reqParams, String requestMethod,
			String encoding, boolean isHttpRequest) throws Exception {
		HttpURLConnection conn = null;
		URL url = new URL(requestUrl);
		conn = (HttpURLConnection) url.openConnection();
		conn.setRequestMethod(requestMethod);
		conn.setDoOutput(true);
		conn.setConnectTimeout(10000);
		conn.setReadTimeout(10000);
		conn.setRequestProperty("User-Agent", "Micah rum");
		String con = "";
		if (isHttpRequest) {
			StringBuffer sb = new StringBuffer();
			for (Map.Entry<String, Object> entry : reqParams.entrySet()) {
				if (sb.length() < 1) {
					sb.append(entry.getKey()).append("=")
							.append(entry.getValue());
				} else {
					sb.append("&").append(entry.getKey()).append("=")
							.append(entry.getValue());
				}
			}
			con = sb.toString();
		} else {
			conn.setRequestProperty("Content-type",
					"application/json; charset=" + encoding);
			con = JsonUtils.objToString(reqParams);
		}
		conn.connect();
		conn.getOutputStream().write(con.getBytes(encoding));
		conn.getOutputStream().flush();
		conn.getOutputStream().close();
		BufferedReader br = new BufferedReader(new InputStreamReader(
				conn.getInputStream(), encoding));
		StringBuffer bs = new StringBuffer();
		String line;
		while ((line = br.readLine()) != null)
			bs.append(line + "\n");
		br.close();
		conn.disconnect();
		// LOG.info(
		// "\n[HttpURLUtils doPost] \nrequestUrl :{}\nreqParams :{}\nencoding :{}\nresult :{}",
		// new Object[] { requestUrl, reqParams, encoding, bs.toString() });
		return bs.toString();
	}

}
